
var swakChatLogFile = null;
var swakChatLogSize = 0;
var swakChatLogMaxSize = 0;
var swakChatLogFormat = null;
var swakChatLogFullHtml = null;
var swakChatLogBuffer = '';
//var swakPreviousChatLogFullHtml = null;
var swakWriteFailExcAck1 = false;

const LTRegExp = /[<]/g;
const GTRegExp = /[>]/g;
//const ANDRegExp = /[&]|[&]amp[;]/g;  
const ANDRegExp = /[&](amp;)?/g;
const TABRegExp = /[\t]/g;


  
   function swakWriteChatLogFile(data,doc, forceClose) {
  //	alert('swakOpenOTSStatsFile');
  	if (data == null || data.innerHTML == null) return;
    if (forceClose == null) forceClose = false;
		var f = null;
		if (swakChatLogMaxSize == null || swakChatLogMaxSize ==0) {
			var maxsize = SS_getPreference(SMACK_CHATLOG_FILEMAXSIZE);
			if (maxsize <0) {
				swakChatLogMaxSize = -1;
			}
			else {
				swakChatLogMaxSize = maxsize*1024;  //size in kB
			}
		}
		
		try {
			var formattedData = data;
			if (swakChatLogFormat == null) {
				swakChatLogFormat = SS_getPreference(SMACK_CHATLOG_OUTPUTFORMAT);
			}
      if (forceClose == true || swakChatLogFile == null || (swakChatLogMaxSize !=-1 && swakChatLogSize >= swakChatLogMaxSize) ) {
      	//alert('create file len ' + swakChatLogSize);
      	
      	swakCloseWriteFile();
      	swakCreateLogFile(swakChatLogFormat);
      	if (swakChatLogFullHtml == null) {
      		swakChatLogFullHtml = SS_getPreference(SMACK_CHATLOG_FULLHTML);
      	}
      	
      	if (swakChatLogFullHtml) {
      		if (swakChatLogFormat == 'html') {
      			swakWriteHtmlLeadIn();
      			
      		}
      		else if (swakChatLogFormat == 'xml') {
      			swakWriteXMLLeadIn();
      			
      		}
      	}
      }
      
      if (swakChatLogFormat == 'html') {
      	formattedData = swakLogFormatHtml(data);
      }
      else if (swakChatLogFormat == 'xml') {
      	formattedData = swakLogFormatXml(data,doc);
      //	alert('formattedData ' + formattedData);
      }
      
      swakChatLogBuffer += formattedData;
      
      var swakChatLogBufferSize = SS_getPreference(SMACK_CHATLOG_CHATLOGBUFFERSIZE);
      if (swakChatLogBuffer.length >= swakChatLogBufferSize) {
      	//alert('writing buffer');
				swakInternalWriteLogFile(swakChatLogBuffer);
				swakChatLogBuffer = '';
      }
      
      

			return true;		
	}
	catch (x) {
		swakAddException(x);
		swakHandleFailedChatLogWrite();
		return false;
	}
	return false;

  	
  	
  }
  
  function swakHandleFailedChatLogWrite() {
  	var ans = confirm("Chat logging failure.  Stop logging (OK) or continnue to try (Cancel)?");
  	if (ans) {
  		SS_prefs.setBoolPref(SMACK_CHATLOG_ENABLED,false);
  		alert('Chat logging disabled due to fault.');
  	}
  }
  
  function swakWriteLogBuffer() {
  	swakInternalWriteLogFile(swakChatLogBuffer);
  	swakChatLogBuffer = '';
  }
  
  function swakInternalWriteLogFile(data) {
  	if (data == null || data == '' || swakChatLogFile == null) return;
  	try {
  	  		var ostream = Components.classes["@mozilla.org/network/file-output-stream;1"]
                        .createInstance(Components.interfaces.nsIFileOutputStream);
		  
				  var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
		                          .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
				  converter.charset = "utf-8"; 
				  var chunk = converter.ConvertFromUnicode(data);
				 // var chunk = converter.ConvertToUnicode(formattedData);
				  
				  
				  
				  var flags = 0x02 | 0x10; //wronly | append
					ostream.init(swakChatLogFile, flags, 0664, 0);
				//	ostream.write(formattedData, formattedData.length);
					ostream.write(chunk, chunk.length);
					var fin = converter.Finish();
		 			if (fin.length > 0) {
		   			ostream.write(fin, fin.length);
  				}
			
			
			ostream.close();
			swakChatLogSize += chunk.length + fin.length; 
		}
		catch(x) {
			alert(x);
		}
  }
  
  function swakCloseLogFile() {
  	if (swakChatLogFile == null) return;
  	try {
  		//alert('close log file');
			//swakWriteChatLogFile(swakLogBuffer);
			swakCloseWriteFile();
		}
		catch(x){ alert(x);}
		swakChatLogFile = null;
		
		
		swakChatLogSize = 0;
		swakChatLogFormat = null;
		swakChatLogFullHtml = null;
		//reset buffer
		//swakLogBuffer = '';
		
  }
  
  function swakCreateLogFile(format) {
  	
  	var fileName = SS_getPreference(SMACK_CHATLOG_OUTPUTDIR);
		if (fileName == null || fileName == '') {
			return;
		}
		var cDate = new Date();
		var ts = cDate.formatDate("YmdHis");
		var ext = '.html';
		if (format == 'xml') {
			ext = '.xml';
		}
		fileName = fileName +"chatlog" + ts + ext;
		var f = null;
		try {
			if (fileName.substring(0,7) == "file://") {
		     // if this is a URL, get the file from that
		     var ioSvc = Components.classes["@mozilla.org/network/io-service;1"].
		                 getService(Components.interfaces.nsIIOService);
		 
		     // XXX it's possible that using a null char-set here is bad
		     const fileUrl = ioSvc.newURI(fileName, null, null).QueryInterface(Components.interfaces.nsIFileURL);
		     f =  fileUrl.file.clone().QueryInterface(Components.interfaces.nsILocalFile);
		   } 
		   else {
		     // if it's a pathname, create the nsILocalFile directly
		      f = new nsLocalFile(fileName);
		 
		    
		   }
	//	   var istream = Components.classes["@mozilla.org/network/file-output-stream;1"]
	//	                        .createInstance(Components.interfaces.nsIFileOutputStream);
		   
		//  var flags = 0x02 | 0x10; //wronly | append
		//	istream.init(f, flags, 0664, 0);
		//	istream.write(data, data.length);
		//	istream.close();
		//reset the buffer
		  swakChatLogBuffer = '';
			f.create(0x00, 0664);
			swakChatLogFile = f;
			swakChatLogSize = 0;
			return true;		
	}
	catch (x) {
		alert('Error creating chat log file: ' + x);
		swakAddException(x);
		throw "WriteCreateFileExc";
	}
	return false;
  	
  }
  
  function swakCloseWriteFile() {
  	if (swakChatLogFile == null) return;
  	//write buffer contents
  	swakInternalWriteLogFile(swakChatLogBuffer);
  	//reset buffer
  	swakChatLogBuffer = '';
  	
  	
  	//alert('close file swakChatLogFormat ' + swakChatLogFormat);
  	if (swakChatLogFullHtml) {
	  	if (swakChatLogFormat == 'html') {
	  		swakWriteHtmlLeadOut();
	  	}
	  	else if (swakChatLogFormat == 'xml') {
	  		swakWriteXMLLeadOut();
	  	}
  	}
  }
  
  function swakLogFormatHtml(commentDiv) {
  	var chatlog = '<div id="' + commentDiv.id + '">' + commentDiv.innerHTML +'</div>';
		chatlog = chatlog.replace(/[/]img[/]/g, 'http://www.jellyfish.com/img/');
	//	chatlog = chatlog.replace(/[/]img[/]shoppers[/]/g, 'http://www.jellyfish.com/img/shoppers/');
		chatlog = chatlog.replace(/[/]smack[/]images[/]/g, 'http://www.jellyfish.com/smack/images/');
		chatlog = chatlog.replace(/\"[/]shoppers[/]/g, '\"http://www.jellyfish.com/shoppers/');
    chatlog +='\n';
		//swakLogBuffer = swakLogBuffer + chatlog + '\n';
  	return chatlog;
  }
  
  function swakLogFormatXml(commentDiv,doc) {
  	var chatNick = getElementChatNick(commentDiv,doc);
  	var nickname = '';
  	var shopperpage = '';
  	if (chatNick != null && chatNick[0] != null) {
  		nickname = chatNick[0].textContent;
  		shopperpage = chatNick[0].href;
  	}
  	var comment = '';
  	var commentTexts = getElementCommentText(commentDiv,doc);
  	if (commentTexts == null || commentTexts[0] == null) {
  		return;
  	}
  	comment = commentTexts[0].textContent;
  	comment = comment.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
  	nickname = nickname.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
  	shopperpage = shopperpage.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
  	
  	var avatar = '';
  	var chatAvatar = getElementChatAvatar(commentDiv,doc);
  	if (chatAvatar != null && chatAvatar[0] != null) {
  		avatar = chatAvatar[0].getAttribute('src');
  	}
  	var guru = '';
  	var chatGuru = getElementChatGuru(commentDiv,doc);
  	if (chatGuru != null && chatGuru[0] != null) {
  		guru = chatGuru[0].textContent;
  		guru = guru.replace(/[()]/g,'');
  	}
  	
  	var largeBadgeImg = '';
  	var largeBadgeText = '';
  	var largeBadge = getElementLargeBadge(commentDiv,doc);
  	if (largeBadge != null && largeBadge[0] != null) {
  		var imgs = largeBadge[0].getElementsByTagName("img");
			for (var i=0;i<imgs.length;i++) {
				largeBadgeText = imgs[i].alt;
				largeBadgeImg = imgs[i].getAttribute('src');
			}
  	}
  	
    var time = '';
  	var chatTime = getElementChatTime(commentDiv,doc);
  	if (chatTime != null && chatTime[0] != null) {
  		time = chatTime[0].textContent;
  	}
  	//var chatlog = '<chatPost id="'+ commentDiv.id + '" nickname="' + nickname + '"><comment>' + commentTexts[0].textContent + '</comment><raw><![CDATA[<div id="' + commentDiv.id + '">' + commentDiv.innerHTML +'</div>]]></raw></chatPost>';
  	var chatlog = '<chatPost><id>' + commentDiv.id + '</id><nickname>' + nickname + '</nickname><shopperpage>' + shopperpage + '</shopperpage><avatar>' + avatar + '</avatar><gurupoints>' + guru + '</gurupoints><time>' + time + '</time><comment>' + comment + '</comment>';
  	if (largeBadgeText != '') {
  		chatlog = chatlog + '<badges><largeBadge><img>' + largeBadgeImg + '</img><text>' + largeBadgeText + '</text></largeBadge></badges>\n';
  	}
  	else {
  		chatlog = chatlog + '<badges/>\n';
  	}
  	if (SS_getPreference(SMACK_CHATLOG_STORERAWPOSTHTML) == true) {
  		chatlog = chatlog + '<raw><![CDATA[<div id="' + commentDiv.id + '">' + commentDiv.innerHTML +'</div>]]></raw>';
  	}
  	chatlog = chatlog + '</chatPost>';
		chatlog = chatlog.replace(/[/]img[/]/g, 'http://www.jellyfish.com/img/');
	//	chatlog = chatlog.replace(/[/]img[/]shoppers[/]/g, 'http://www.jellyfish.com/img/shoppers/');
		chatlog = chatlog.replace(/[/]smack[/]images[/]/g, 'http://www.jellyfish.com/smack/images/');
		chatlog = chatlog.replace(/\"[/]shoppers[/]/g, '\"http://www.jellyfish.com/shoppers/');
		chatlog += '\n\r';
  	//swakLogBuffer = swakLogBuffer + chatlog + '\n\r';
  	return chatlog;
  }
  
  function swakWriteHtmlLeadIn() {
  	if (swakChatLogFile == null) return;
  	try {
  		var userheader = SS_getPreference(SMACK_CHATLOG_HEADER);
  		var leadin = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link type="text/css" rel="Stylesheet" media="screen" href="http://www.jellyfish.com/smack/stylesheets/smackV2.css?1200327926"/><link type="text/css" rel="Stylesheet" media="screen" href="http://www.jellyfish.com/smack/stylesheets/smackV2_static.css?1183673490"/>' + userheader + '</head>';
  		leadin = leadin + '<table cellspacing="0" cellpadding="0" style="width: 100%;"><tbody><tr><td id="rightPanel" valign="top">';
  		leadin = leadin + '<div id="chatContainer" class="chatView"><div id="commentsBox"><div id="chat">';
  		var istream = Components.classes["@mozilla.org/network/file-output-stream;1"]
		                        .createInstance(Components.interfaces.nsIFileOutputStream);
		   
		  var flags = 0x02 | 0x10; //wronly | append
			istream.init(swakChatLogFile, flags, 0664, 0);
			istream.write(leadin, leadin.length);
			istream.close();
  	}
  	catch(x) {
  		
  		if (swakWriteFailExcAck1 == false) {
  			alert('Unable to write chat log html lead in info: Exception ' + x);
  			swakWriteFailExcAck1 = true;
  		}
  		throw 'WriteFailExc';
  	}
  }
  
   function swakWriteHtmlLeadOut() {
   	if (swakChatLogFile == null) return;
  	try {
  		var leadout = '</div></div></div></td></tr></tbody></table></html>';
  		var istream = Components.classes["@mozilla.org/network/file-output-stream;1"]
		                        .createInstance(Components.interfaces.nsIFileOutputStream);
		   
		  var flags = 0x02 | 0x10; //wronly | append
			istream.init(swakChatLogFile, flags, 0664, 0);
			istream.write(leadout, leadout.length);
			istream.close();
  	}
  	catch(x) {
  		var exc = 'Unable to write chat log html lead out info: Exception ' + x;
  		if (swakWriteFailExcAck1 == false) {
  			alert(exc);
  			swakWriteFailExcAck1 = true;
  		}
  		throw 'WriteFailExc';
  	}
  }
  
  function swakWriteXMLLeadIn() {
   	if (swakChatLogFile == null) return;
  	try {
  		var userheader = SS_getPreference(SMACK_CHATLOG_HEADER);
  		var leadin = '<?xml version="1.0" encoding="UTF-8"?>' + userheader + '<chatPosts>';
  		var istream = Components.classes["@mozilla.org/network/file-output-stream;1"]
		                        .createInstance(Components.interfaces.nsIFileOutputStream);
		   
		  var flags = 0x02 | 0x10; //wronly | append
			istream.init(swakChatLogFile, flags, 0664, 0);
			istream.write(leadin, leadin.length);
			istream.close();
  	}
  	catch(x) {
  		if (swakWriteFailExcAck1 == false) {
  			alert('Unable to write chat log xml lead in info: Exception ' + x);
  			swakWriteFailExcAck1 = true;
  		}
  		throw 'WriteFailExc';
  	}
  } 
  
  function swakWriteXMLLeadOut() {
  	//alert('swakWriteXMLLeadOut swakChatLogFile ' + swakChatLogFile);
   	if (swakChatLogFile == null) return;
  	try {
  		var leadout = '</chatPosts>';
  		var istream = Components.classes["@mozilla.org/network/file-output-stream;1"]
		                        .createInstance(Components.interfaces.nsIFileOutputStream);
		   
		  var flags = 0x02 | 0x10; //wronly | append
			istream.init(swakChatLogFile, flags, 0664, 0);
			istream.write(leadout, leadout.length);
			istream.close();
  	}
  	catch(x) {
  		if (swakWriteFailExcAck1 == false) {
  			alert('Unable to write chat log xml lead out info: Exception ' + x);
  			swakWriteFailExcAck1 = true;
  		}
  		throw 'WriteFailExc';
  	}
  }
  

////////////////////////////////////
// deal logging
////////////////////////////////////

	var swakDealBuffer = '';
  function swakWriteDealStatsFile(doc) {
  //	alert('swakOpenOTSStatsFile');
  	if (doc == null) return;
  	var fileName = SS_getPreference(SMACK_DEALLOG_OUTPUTFILENAME);
		if (fileName == null || fileName == '') {
			return;
		}
		var f = null;
		try {
			if (fileName.substring(0,7) == "file://") {
		     // if this is a URL, get the file from that
		     var ioSvc = Components.classes["@mozilla.org/network/io-service;1"].
		                 getService(Components.interfaces.nsIIOService);
		 
		     // XXX it's possible that using a null char-set here is bad
		     const fileUrl = ioSvc.newURI(fileName, null, null).QueryInterface(Components.interfaces.nsIFileURL);
		     f =  fileUrl.file.clone().QueryInterface(Components.interfaces.nsILocalFile);
		   } 
		   else {
		     // if it's a pathname, create the nsILocalFile directly
		      f = new nsLocalFile(fileName);
		 
		    
		   }
		   var ostream = Components.classes["@mozilla.org/network/file-output-stream;1"]
		                        .createInstance(Components.interfaces.nsIFileOutputStream);
		  
		  swakDealLogFormatXml(doc); 
		 // var createFile = false;

		  //	var flags = 0x02 | 0x10; //wronly | append
			  var flags = 0x02 | 0x10 | 0x08; //wronly | append | create if does not exist
				ostream.init(f, flags, 0664, 0);
			
			
			var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
                          .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
		  converter.charset = "utf-8"; 
		  
		  var chunk = converter.ConvertFromUnicode(swakDealBuffer);

			//ostream.write(swakDealBuffer, swakDealBuffer.length);
			ostream.write(chunk, chunk.length);
			
			var fin = converter.Finish();
 			if (fin.length > 0) {
   			ostream.write(fin, fin.length);
  		}
			
			ostream.close();
			swakDealBuffer = '';
			return true;		
	}
	catch (x) {
		alert(x);
		swakAddException(x);
		swakDealBuffer = '';
		return false;
	}
	return false;

  	
  	
  }
  
  function swakHandleFailedDealWrite() {
  	var ans = confirm("Deal logging failure.  Stop logging (OK) or continnue to try (Cancel)?");
  	if (ans) {
  		SS_prefs.setBoolPref(SMACK_DEALLOG_ENABLED,false);
  		alert('Deal logging disabled due to fault.');
  	}
  }
  
  
  function swakDealLogFormatXml(data,doc) {
  	swakDealBuffer = '';
  	var ggwinners = swaksmack.SS_JF.getGuruGuessWinners();
  	//var ggwinners = swaksmack.SS_JF.getGuruGuessWinnersFromHTML();
  	var retailPrice = swaksmack.SS_JF.getRetailPrice();
		var discountPercent = swaksmack.SS_JF.getDiscount();
		var salePrice = swaksmack.SS_JF.getSalePrice(retailPrice,discountPercent);
		var pid = swaksmack.SS_JF.getProductId();
		var pname = swaksmack.SS_JF.getProductName();
		var pdesc = swaksmack.SS_JF.getProductDescription();
		var pcatid = swaksmack.SS_JF.getProductCategoryId();
		var pimg = swaksmack.SS_JF.getProductImg();
		var showName = swaksmack.SS_JF.getShowName();
		
		var mid = swaksmack.SS_JF.getMerchantId();
		var mname = swaksmack.SS_JF.getMerchantName();
		var jfd = swaksmack.SS_JF.getMerchantJFDirect();
		var mimg = swaksmack.SS_JF.getMerchantImg(); 
		
		var starttime = swaksmack.SS_JF.getDealStartedAt();
		var soldtime = swaksmack.SS_JF.getDealSoldOutAt();
		
		var starttimeD = new Date(starttime);
		var soldtimeD = new Date(soldtime);
		
		var buyers = swaksmack.SS_JF.getBestDeals();
		var numSold = 0;
		if (buyers != null) {
			numSold = buyers.length;
		}
		
		starttimeD = starttimeD.formatDate("r");
		
		soldtimeD = soldtimeD.formatDate("r");
		
		//tough call here...I've seen some products with the & char already escaped
		//todo or not todo
		
		try {
			pname = pname.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
			pdesc = pdesc.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
			showName = showName.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
			pimg = pimg.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
			pimg = 'http://www.jellyfish.com/img' + pimg;
			mname = mname.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
			mimg = mimg.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
			mimg = 'http://www.jellyfish.com/img' + mimg;
		//	alert('merchant img ' + mimg);
			if (buyers != null && buyers.length>0) {
				for (var i=0;i<buyers.length;i++) {
					if (buyers[i].nickname) {
						buyers[i].nickname = buyers[i].nickname.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
					}
					if (buyers[i].city) {
						buyers[i].city = buyers[i].city.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
					}
					if (buyers[i].state) {
						buyers[i].state = buyers[i].state.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
					}
					if (buyers[i].avatarUri) {
						buyers[i].avatarUri = buyers[i].avatarUri.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
						buyers[i].avatarUri = 'http://www.jellyfish.com' + buyers[i].avatarUri;
					}
					if(buyers[i].shopperPageName) {
						buyers[i].shopperPageName = 'http://www.jellyfish.com/shoppers/' +buyers[i].shopperPageName;
					}
				}
			}
			if (ggwinners != null && ggwinners.length>0) {
				for (var i=0;i<ggwinners.length;i++) {
					ggwinners[i].nickname = ggwinners[i].nickname.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
			//		ggwinners[i].city = ggwinners[i].city.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
			//		ggwinners[i].state = ggwinners[i].state.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
			//		ggwinners[i].avatarUri = ggwinners[i].avatarUri.replace(LTRegExp, "&lt;").replace(GTRegExp, "&gt;").replace(ANDRegExp,"&amp;").replace(TABRegExp, "    ");
				}
			}			
		}
		catch (x) {
			alert(x);
		}	
		
		
		var deallog = '<deal><starttime epochtime="' + starttime + '">' + starttimeD + '</starttime><endtime epochtime="' + soldtime + '">' + soldtimeD + '</endtime><showname>' + showName + '</showname><numberSold>' + numSold + '</numberSold>\n';
		deallog = deallog + '<price><retail>' + retailPrice + '</retail><bestDiscount>' + discountPercent + '</bestDiscount><bestDealPrice>' + salePrice + '</bestDealPrice></price>\n';
		deallog = deallog + '<product><id>' + pid + '</id><category>' + pcatid + '</category><name>' + pname + '</name><description>' + pdesc + '</description><img>' + pimg + '</img></product>\n';
		deallog = deallog + '<merchant><id>' + mid + '</id><name>' + mname + '</name><img>' + mimg + '</img><jfdirect>' + jfd + '</jfdirect></merchant>\n';
		deallog = deallog + '<buyers>\n';
		if (buyers != null && buyers.length>0) {
			for (var i=0;i<buyers.length;i++) {
				try {
					deallog = deallog + '<buyer><nickname>' + buyers[i].nickname + '</nickname><avatar>' + buyers[i].avatarUri + '</avatar><city>' + buyers[i].city + '</city><state>' + buyers[i].state + '</state><discount>' + buyers[i].discount + '</discount><rank>' + buyers[i].rank + '</rank><shopperpage>' + buyers[i].shopperPageName + '</shopperpage></buyer>\n';
				}
				catch(x) {
				}
			}
		}
		deallog = deallog + '</buyers>\n';
		if (ggwinners != null && ggwinners.length>0) {
			deallog = deallog + '<guruguesswinners>';
			for (var i=0;i<ggwinners.length;i++) {
				try {
					deallog = deallog + '<guesser><nickname>' + ggwinners[i].nickname + '</nickname><points>' + ggwinners[i].points + '</points><guess>' + ggwinners[i].guess + '</guess></guesser>\n';
				}
				catch(x) {
				}
			}
			deallog = deallog + '</guruguesswinners>\n';
		}
		else {
			deallog = deallog + '<guruguesswinners/>\n';
		}
    deallog = deallog + '</deal>\n';
    //deallog = deallog.replace(/[/]img[/]/g, 'http://www.jellyfish.com/img/');
		//deallog = deallog.replace(/[/]img[/]shoppers[/]/g, 'http://www.jellyfish.com/img/shoppers/');
		//deallog = deallog.replace(/[/]smack[/]images[/]/g, 'http://www.jellyfish.com/smack/images/');
		//deallog = deallog.replace(/\"[/]shoppers[/]/g, '\"http://www.jellyfish.com/shoppers/');
    
    
    swakDealBuffer = deallog;
  }